package com.cqq.print.business.service.impl;


import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqq.print.business.dao.PCptProcessDao;
import com.cqq.print.business.dao.PProduceDao;
import com.cqq.print.business.dao.domain.PCptProcess;
import com.cqq.print.business.dao.domain.PProduce;
import com.cqq.print.business.service.bean.PCptProcessBean;
import com.cqq.print.constant.Constant;


/**
 * @author generated
 * @date 2015-07-01
 */


@Service(value="pCptProcessService")
public class PCptProcessServiceImpl implements com.cqq.print.business.service.PCptProcessService{

             @Autowired
             private PCptProcessDao pCptProcessDao;

             @Autowired
             private PProduceDao pProduceDao;
             
			
			public PCptProcessBean queryCPTProcessByProId(int proId) {
				PCptProcess cpt = pCptProcessDao.queryCPTProcessByProId(proId);
				if(cpt!=null){
					PCptProcessBean cptBean = new PCptProcessBean();
					BeanUtils.copyProperties(cpt, cptBean);
					return cptBean;
				}
				return null;
			}

			
			public void savePCPTProcess(PCptProcessBean cptBean) {
				if(cptBean!=null&&cptBean.getProId()!=null){
					PCptProcess cpt = new PCptProcess();
					BeanUtils.copyProperties(cptBean, cpt);
					if(cpt.getId()==null||cpt.getId()<=0){
						pCptProcessDao.insertSelective(cpt);
					}else{
						pCptProcessDao.updateByPrimaryKeySelective(cpt);
					}
					//更新生产任务的cpt状态
					if(isAllChecked(cpt)){
						PProduce pp = new PProduce();
						pp.setId(cpt.getProId());
						pp.setCptCheck(Constant.PRODUCE_CPTCHECK_DONE);
						pp.setUpdateDate(new Date());
						pProduceDao.updateByPrimaryKeySelective(pp);
					}
				}
				
			}

			private boolean isAllChecked(PCptProcess cpt){
				if(cpt.getArticlesizeCover()==null||cpt.getArticlesizeCover()<1)
					return false;
				if(cpt.getCoverpriceCopy()==null||cpt.getCoverpriceCopy()<1)
					return false;
				if(cpt.getCovernameFlyleaf()==null||cpt.getCovernameFlyleaf()<1)
					return false;
				if(cpt.getPagenumStamp()==null||cpt.getPagenumStamp()<1)
					return false;
				if(cpt.getArticleCheck()==null||cpt.getArticleCheck()<1)
					return false;
				if(cpt.getImagedebug()==null||cpt.getImagedebug()<1)
					return false;
				if(cpt.getHeimaCheck()==null||cpt.getHeimaCheck()<1)
					return false;
				if(cpt.getPrintDisk()==null||cpt.getPrintDisk()<1)
					return false;
				return true;
			}
}